
#include "MenuSystem.h"
#include "IrControl.h"
#include "Menu/Tool/frame.h"

#define ITEM_DISDEF_YES	(0)
#define ITEM_DISDEF_NO		(1)

#define  ITEM_DISDEF_BUTTON_QTY	(2)

TRect		VideoDispDefTouchRect[ITEM_DISDEF_BUTTON_QTY];
TPosCoor		VideoDispDefInfoPos= {230,126};//{230,126,475,201};
TPosCoor	VideoDispDefItemPos[ITEM_DISDEF_BUTTON_QTY] = { {126,222}, {378,222}};
int		VideoDispDefFocus = -1;
TFrame		VideoDispDefFrame;
char		*VideoDispDefInfoStr[ITEM_DISDEF_BUTTON_QTY] ={"",""};// {"YES","NO"};
//unsigned char BASTRE_data [5] = {0xe0,0x03,0x06,0x08,0xF1};
extern	unsigned char read_info_data[10];
extern int volume_count ;
extern int volume_temp;
extern unsigned char mute_status;
extern unsigned char	VOLvalue ;
extern struct _VolumePos VolumePos;
extern TRect	BarInfoRect;
extern unsigned char rece_handle;	
extern unsigned char dtpresent_flag;
extern pthread_cond_t dt_cond;


int  VideoDispDefInfoUpdata (void)
{
 return 0;
}

void VideoDispDefDrawBackgroup(TCanvas *pCanvas)
{
	TJpeg pJpeg;
	TPosCoor pos;
	char FileName[20];
	int GetJpegIdx;

	GetJpegIdx = IMG_VIDEO_DISPDEF_INFOCOVER;
	pos = VideoDispDefInfoPos;
	
	DrawJpeg(pCanvas, 0, 0, "/res_jpeg/bkg.jpg" );
	DrawJpeg(pCanvas, 0, 8, "/res_jpeg/bar.jpg" );
	sprintf( FileName, "res_jpeg/%d/%d.jpg", VIDEO_DISPDEF_FOLDER, GetJpegIdx);
	DrawJpeg(pCanvas, pos.x, pos.y, FileName);
	g_Puts( pCanvas, FONT33, BarInfoRect, TEXT_ALIGN_CENTER, "VIDEO DEFAULT" );


}


void VideoDispDefDrawVolume( TCanvas *pCanvas,int flag)
{
	int GetJpegIdx;
	TRect rect;
	TJpeg pJpeg;
	char FileName[20];
	int x,y;
	int i;
	switch (flag)
		{							
			case 1://disp volume all
				GetJpegIdx = IMG_VOLUME_BARCOVER;
				sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);	
				DrawJpeg(pCanvas, VolumePos.Volume_all.x, VolumePos.Volume_all.y, FileName);	
				if (mute_status == 1)
					{
						GetJpegIdx = IMG_VOLUME_MUTE ;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						DrawJpeg(pCanvas, VolumePos.Volume_speaker.x, VolumePos.Volume_speaker.y, FileName);	
					}	
				else
						mute_status = 2;
				
				for (i = 0; i<VOLvalue; i++)
					{
					if (i%2)
						{
						x = VolumePos.Volume_bar_start.x+VolumePos.Distance*(i/2) + VolumePos.Barwidth;
						GetJpegIdx = IMG_VOLUME_RIGHTFOCUS;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						}
					else
						{
						x = VolumePos.Volume_bar_start.x+VolumePos.Distance*(i/2) ;
						GetJpegIdx = IMG_VOLUME_LEFTFOCUS;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						}
					y = VolumePos.Volume_bar_start.y;					
					DrawJpeg(pCanvas,x, y, FileName);						
					}
			break;			
			
			case 2://draw bar
			
			break;
			
			case 3://undisp
				DrawJpeg(pCanvas, 0, 8, "/res_jpeg/bar.jpg" );
				g_Puts( pCanvas, FONT33, BarInfoRect, TEXT_ALIGN_CENTER, "VIDEO DEFAULT" );
			break;
		}
	
	pJpeg = GetJpegPtr("/res_jpeg/bar.jpg" );
	rect.left = BARPOS_X;
	rect.right = rect.left + pJpeg.Width;
	rect.top = BARPOS_Y;
	rect.bottom = rect.top + pJpeg.Height;	
	dt_Present_part(rect);		
	volume_count = 0;
	volume_temp = 0;

}


static void VolumeCountHandle(void)
{
	volume_count ++;
		if (volume_count >0xFF00)
			{
				volume_temp ++;
				volume_count = 0;
			}
		if (volume_temp > 10)
			{
				VideoDispDefDrawVolume(VideoDispDefFrame.pCanvas,3);					
				volume_count = 0;	
				volume_temp = 0;	
				mute_status =0;
			}
}

void VideoDispDefDrawItem( TCanvas *pCanvas, int idx, BOOL bfocus )
{
	TJpeg pJpeg;
	TPosCoor pos;
	char FileName[20];
	int GetJpegIdx;

	if( idx < 0 || idx >= IMG_VIDEO_DISPDEF_ITEM_QTY )
		return;

	if( bfocus ){
		GetJpegIdx = IMG_VIDEO_DISPDEF_ITEM_FOCUS_YES+idx;
	}else{
		GetJpegIdx = IMG_VIDEO_DISPDEF_ITEM_YES +idx ;
	}
	pos = VideoDispDefItemPos[idx];
	sprintf( FileName, "res_jpeg/%d/%d.jpg", VIDEO_DISPDEF_FOLDER, GetJpegIdx);	
	pJpeg = GetJpegPtr(FileName);
	DrawJpeg(pCanvas, pos.x, pos.y, FileName);
	VideoDispDefTouchRect[idx].left = pos.x;
	VideoDispDefTouchRect[idx].right = pos.x + pJpeg.Width;
	VideoDispDefTouchRect[idx].top = pos.y;
	VideoDispDefTouchRect[idx].bottom = pos.y + pJpeg.Height;
	g_Puts( pCanvas, FONT33, VideoDispDefTouchRect[idx], TEXT_ALIGN_CENTER, VideoDispDefInfoStr[idx] );

	
}


void VideoDispDefDraw( BOOL bDrawAll )
{
	int i;
	
	if( bDrawAll )
		VideoDispDefDrawBackgroup(VideoDispDefFrame.pCanvas);

	for( i = 0; i < ITEM_DISDEF_BUTTON_QTY; i++ )
		VideoDispDefDrawItem( VideoDispDefFrame.pCanvas, i, FALSE );
	if (mute_status == 1)
		{
		VideoDispDefDrawVolume(VideoDispDefFrame.pCanvas,1);
		}
	dtpresent_flag++;		
	pthread_cond_signal(&dt_cond);
	//dt_Present();
}


void VideoDispDefInit()
{
	ir_SetCurrentID( IR_VIDEO_DEF );
	
	frm_Create( &VideoDispDefFrame, FRM_NO_TITLE|FRM_NO_TASK, "" );		
	VideoDispDefDraw( TRUE );		
}

int VideoDispDefGetTouchID( TPosCoor pos )
{
	int i;
	for( i = 0; i < ITEM_DISDEF_BUTTON_QTY; i++ )
		if( ut_PtInRect( pos,  VideoDispDefTouchRect[i] ) )
			return i;
	return -1;
}


BOOL VideoDispDefBtnEnter(int idx)
{
	switch(idx)
	{
		case ITEM_DISDEF_YES:
		case ITEM_DISDEF_NO:
			MenuVideoInit();
			break;
	
	}
	return TRUE;
}



/*****************************
function:message_dispatch

 return:
  0: successful
  1: need other menu handle
  -1:failure
*****************************/
static int message_dispatch (void)
{
	int rc = 1;
	
	switch (read_info_data[2])
	{
					case	ID_POWER:						
						break;
						
					case	ID_DISPLAY_SCREEN:	
							switch (read_info_data[3])
								{
									case 	ID_SCREEN_MENU:
											MenuMainInit();
										break;		
									case 	ID_SCREEN_RADIO:
											MenuRadioInit();
										break;									
									case	ID_SCREEN_CDLOAD:											
									case	ID_SCREEN_CDDISP:
									case	ID_SCREEN_MP3DISP:
									case	ID_SCREEN_NODISC:
									case	ID_SCREEN_DISCERR:
									case	ID_SCREEN_EJECT:
											MenuCdInit();
											rece_handle = 0;
										break;
									case	ID_SCREEN_MEDIEA:
											MenuMediaInit();
										break;
									
									case 	ID_SCREEN_NAVI:
										MenuNaviInit();
										break;	
										
									case	 	ID_SCREEN_AUDIODISP:
											MenuAudioInit();
											break;	
									case		ID_SCREEN_VIDEODISP:
										MenuVideoInit();
										break;	
									case		ID_SCREEN_EQDISP:
											AudioEQInit();
										break;	
									case	 	ID_SCREEN_BASTRA:
											AudioBASInit();
										break;	
									case		ID_SCREEN_FADBAL:	
											AudioFADInit();							
										break;	
									}
									rc = 0;//if handled, clear this flag.	
							read_info_data[0] = 0;											
						break;
						
					case	ID_KEY_STATUS:						
						break;
						
					case	ID_SCREEN_INFO:
						if (read_info_data[3] == ID_SCREEN_INFO_VOLUME)
								{
									VideoDispDefDrawVolume(VideoDispDefFrame.pCanvas,1);
								}
									//ReadInfoKey = NO_INFO; //if handled, clear this flag.
									//rc = 0;//if handled, clear this flag.
								break;
						
					case	ID_KEYPAD:	
						switch (read_info_data[3])
							{
									case 	ID_MUTE:
										VideoDispDefDrawVolume(VideoDispDefFrame.pCanvas,1);
										break;
									case	ID_DMUTE:	
										VideoDispDefDrawVolume(VideoDispDefFrame.pCanvas,3);
										break;									
									case 	ID_SEEKUP:
									case 	ID_SEEKDN:
										break;
										
									case 	ID_DEST:
									case 	ID_NAVI:
										MenuNaviInit();
										break;
										
									case 	ID_FMAM:
										MenuRadioInit();
										break;
									case 	ID_CD:
										MenuCdInit();
										break;				
									case 	ID_MEDIA:
										MenuMediaInit();								
										break;
									case 	ID_MENU:
										MenuMainInit();
										break;
										
									case	ID_TVAUX:
										
										break;
							}	
							rc = 0;//if handled, clear this flag.	
							read_info_data[0] = 0;
						break;
					case 	ID_BAND_SEL:
							MenuRadioInit();
							rc = 0;//if handled, clear this flag.	
							read_info_data[0] = 0;
						break;	
						
					default:	
							rc = -1;						
						}	
	return 0;
}


void VideoDispDefInput(UInt32 key)
{
			int flag;
	
		if ( key == IRC_READ_INFO)
		{
			flag =message_dispatch ();
			if (flag == -1)
				printf ("main menu handle receive data error!\n");
			if (flag == 0)
				{
					//printf ("main menu handle receive data success!\n");
					return;
				}			
		}
		
	if( key == IRC_TOUCH_DOWN || key == IRC_TOUCH_UP )
	{
		int focus;
		TPosCoor pos;
		dt_ClipTouchGetXY( &pos.x, &pos.y );
		if( key == IRC_TOUCH_DOWN )
		{
			if( LastKey ==IRC_TOUCH_UP )
			{
				if( ( VideoDispDefFocus =VideoDispDefGetTouchID( pos )) != -1 )
				{
					VideoDispDefDrawItem( VideoDispDefFrame.pCanvas, VideoDispDefFocus, TRUE );
					dt_Present_part(VideoDispDefTouchRect [VideoDispDefFocus]);						
				}
			}
		}
		else
		{
			if( ( focus = VideoDispDefGetTouchID( pos ) ) == VideoDispDefFocus )
				VideoDispDefBtnEnter( VideoDispDefFocus );
			else if( VideoDispDefFocus != -1 )
			{
				dt_Present_part(VideoDispDefTouchRect [VideoDispDefFocus]);
			}
			VideoDispDefFocus = -1;
		}
		LastKey = key;//hangsheng add 071126
	}
	if(mute_status ==2) 
	VolumeCountHandle ();
}
